﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using Tesla.Graphics;
using Tesla.Graphics.Implementation;
using D3D = SlimDX.Direct3D10;

namespace Tesla.Direct3D10.Graphics.Implementation {
    /// <summary>
    /// Concrete Direct3D10 implementation for <see cref="BlendState"/>.
    /// </summary>
    public sealed class D3D10BlendStateImplementation : BlendStateImplementation {
        private D3D10Renderer _renderer;
        private D3D.Device _graphicsDevice;
        private D3D.BlendState _bs;

        /// <summary>
        /// Gets the D3D10Renderer that created this implementation.
        /// </summary>
        internal D3D10Renderer Renderer {
            get {
                return _renderer;
            }
        }

        /// <summary>
        /// Gets the actual D3D10 BlendState object.
        /// </summary>
        internal D3D.BlendState D3D10BlendState {
            get {
                return _bs;
            }
        }

        /// <summary>
        /// Gets the number of render targets that blending can be enabled for.
        /// </summary>
        public override int BlendEnableCount {
            get {
                return 8;
            }
        }

        /// <summary>
        /// Gets the number of render targets that can have an independent write mask for.
        /// </summary>
        public override int ColorWriteChannelsCount {
            get {
                return 8;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="D3D10BlendStateImplementation"/> class.
        /// </summary>
        /// <param name="renderer">The renderer.</param>
        internal D3D10BlendStateImplementation(D3D10Renderer renderer) : 
            base() {
            _renderer = renderer;
            _graphicsDevice = _renderer.GraphicsDevice;
        }

        /// <summary>
        /// Binds the implementation. This is called the first time an unbound state is set to the device or manually by the user in order to
        /// create the underlying state ahead of time (best practice). Once called the state properties are read-only.
        /// </summary>
        public override void BindBlendState() {
            if(!base.IsBound) {
                D3D.BlendStateDescription desc = new D3D.BlendStateDescription();
                desc.AlphaBlendOperation = D3D10Helper.ToD3DBlendOperation(base.AlphaBlendFunction);
                desc.SourceAlphaBlend = D3D10Helper.ToD3DBlendOption(base.AlphaSourceBlend);
                desc.DestinationAlphaBlend = D3D10Helper.ToD3DBlendOption(base.AlphaDestinationBlend);

                desc.BlendOperation = D3D10Helper.ToD3DBlendOperation(base.ColorBlendFunction);
                desc.SourceBlend = D3D10Helper.ToD3DBlendOption(base.ColorSourceBlend);
                desc.DestinationBlend = D3D10Helper.ToD3DBlendOption(base.ColorDestinationBlend);

                for(int i = 0; i < 8; i++) {
                    desc.SetBlendEnable((uint)i, this.GetBlendEnable(i));
                    desc.SetWriteMask((uint)i, (D3D.ColorWriteMaskFlags) this.GetWriteChannels(i));
                }

                _bs = D3D.BlendState.FromDescription(_graphicsDevice, desc);

                base.IsBound = true;

                //Add the SlimDX object to the tracker
                _renderer.Resources.AddTrackedObject(_bs.ComPointer, this);
            }
        }

        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="D3D10BlendStateImplementation"/> is reclaimed by garbage collection.
        /// </summary>
        ~D3D10BlendStateImplementation() {
            Dispose(false);
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing) {
            if(!IsDisposed) {
                //Dispose of managed resources
                if(disposing) {
                    if(_bs != null) {
                        //Remove from resource tracker
                        if(_renderer != null) {
                            _renderer.Resources.RemoveTrackedObject(_bs.ComPointer);
                        }
                        _bs.Dispose();
                        _bs = null;
                    }
                }
                base.Dispose(disposing);
            }
        }
    }
}
